package com.nutransit;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;

import com.mylibrary.mylibrary.parsing.AsyncTaskInterface;
import com.mylibrary.mylibrary.parsing.AsyncTaskUtility;
import com.mylibrary.mylibrary.util.CommonCode;
import com.nutransit.commoncode.Webservice_parametrs;

public class Fragment_Car_Van_Details_Request extends ActionBarActivity implements OnClickListener, Webservice_parametrs
{
	private Context context;
	
	private HashMap<String, String> hashMap;
	private String strRequestId = null, strUserPhoneNo = null;
	private int position;
	
	private TextView tv_Source, tv_Destination, tv_Price;
	private ActionBar actionBar;
	
	@SuppressLint("InlinedApi")
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		if(Build.VERSION.SDK_INT>=11)
			getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
		setContentView(R.layout.activity_details_booking);
		init_var();
	}
	private void init_var()
	{
		context = Fragment_Car_Van_Details_Request.this;
		actionBar = getSupportActionBar();
		position = getIntent().getIntExtra(TAG_pageid, -1);
		
		tv_Source = (TextView) findViewById(R.id.tv_Source);
		tv_Destination = (TextView) findViewById(R.id.tv_Destination);
		tv_Price = (TextView) findViewById(R.id.tv_Price);
		actionBar.setIcon(new ColorDrawable(android.R.color.transparent));
		actionBar.setBackgroundDrawable(new ColorDrawable(android.R.color.transparent));
		actionBar.setTitle("");
		
		findViewById(R.id.btn_ContactPassenger).setOnClickListener(this);
		findViewById(R.id.btn_PickedUp).setOnClickListener(this);
		findViewById(R.id.btn_CANCEL).setOnClickListener(this);
		
		Intent intent = getIntent();
		if(intent!=null && intent.hasExtra(TAG_data))
		{
			String strData = intent.getStringExtra(TAG_data);
			if(strData!=null && strData.length()>0)
			{
				try
				{
					JSONObject jsonObject = new JSONObject(strData);
					strRequestId = jsonObject.getString(TAG_idprimary);
					strUserPhoneNo = jsonObject.getString(TAG_phone1);
					if(strUserPhoneNo.length()==0)
					{	strUserPhoneNo = jsonObject.getString(TAG_phone2);
						if(strUserPhoneNo.length()==0)
							strUserPhoneNo = jsonObject.getString(TAG_phone2);
					}
						
					tv_Price.setText("$"+CommonCode.formatTo2Digit(jsonObject.getString(TAG_calculatedprice)));
					tv_Source.setText(jsonObject.getString(TAG_startpoint)+", "+jsonObject.getString(TAG_startcity)+", "+jsonObject.getString(TAG_startstate));
					tv_Destination.setText(jsonObject.getString(TAG_endpoint)+", "+jsonObject.getString(TAG_endcity)+", "+jsonObject.getString(TAG_endstate));
				}
				catch (JSONException e)
				{
					e.printStackTrace();
				} 
			}
		}
	}
	@Override
	public void onClick(View view)
	{
		switch (view.getId())
		{
			case R.id.btn_ContactPassenger:
				if(strUserPhoneNo!=null)
					call_customer();
				break;
			case R.id.btn_PickedUp:
				if(strRequestId!=null)
					accept_or_reject_request(strRequestId, "1");
				break;
			case R.id.btn_CANCEL:
				if(strRequestId!=null)
					accept_or_reject_request(strRequestId, "2");
				break;
	
			default:
				break;
		}
	}
	private void call_customer()
	{
		Intent intent = new Intent(Intent.ACTION_CALL);
		intent.setData(Uri.parse("tel:" + strUserPhoneNo));
		context.startActivity(intent);
	}
	@Override
	public void onBackPressed()
	{
//		super.onBackPressed();
	}
	
	protected void accept_or_reject_request(String strRequestId, String str_accept_or_reject)
	{
		hashMap = new HashMap<String, String>();
		hashMap.put(TAG_requestid, strRequestId);
		hashMap.put(TAG_flag, str_accept_or_reject);
		new AsyncTaskUtility(context, hashMap, URL_ACCEPT_OR_REJECT_REQUEST).getJSONResponse(new AsyncTaskInterface()
		{
			@Override
			public void getJSONObjectFromAsynkTask(JSONObject json)
			{
				try
				{
					if(json!=null)
					{
						if(json.has(TAG_message))
							CommonCode.showMessage(json.getString(TAG_message), context);
						if(json.getString(TAG_status).equalsIgnoreCase(SUCCESS))
						{
							Intent intent = new Intent();
							intent.putExtra(TAG_pageid, position);
							setResult(Fragment_User_Request.REQUEST_CODE_REQUEST_DETAILS, intent);
							Fragment_Car_Van_Details_Request.this.finish();
						}
					}
				}
				catch(JSONException e)
				{
					e.printStackTrace();
				}
			}
		});
	}
}
